package com.itheima;

import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {
    int a=1;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        Map<String, String[]> map = request.getParameterMap();
        //创建User对象
        User u=new User();
        //把请求的参数封装成对象
        u.setId(a);
        a++;
        try {
            BeanUtils.populate(u,map);
        } catch (Exception e) { }
        //调用类保存数据
        UserDao dao=new UserDao();
        dao.addUser(u);
        //跳转到展示地方
        request.getRequestDispatcher("/findServlet").forward(request,response);
    }
}
